import { CallHandler, ExecutionContext, Injectable, Logger, NestInterceptor } from '@nestjs/common';
import { Request } from 'express';
import { map } from 'rxjs/operators';

@Injectable()
export class TransformInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler) {
    // console.log('拦截器前');
    const request = context.switchToHttp().getRequest() as Request;
    const startTime = Date.now();
    return next.handle().pipe(
      map((data) => {
        // const endTime = Date.now();
        // new Logger().error(`TIME:${endTime - startTime}\tURL:${request.path}\tMETHOD:${request.method}`);

        // 这里对返回的数据进行处理 所有的的数据格式将被 data:{数据} 包裹
        return data?.meta ? data : { data };
      }),
    );
  }
}
